const express = require("express");
// 引入功能中间件
const cors = require("./tools/cors");
const cookieParser = require("cookie-parser");
const session = require("express-session");
// 引入文件上传中间件
const multer = require("multer");
// 实例化文件上传中间件，并制定文件上传后的存储路径
const upload = multer({dest: "./upload"});

const verify = require("./tools/verify");

// 引用路由中间件
const login = require("./router/login");
const register = require("./router/register");
const getUser = require("./router/getUser");

const setUser = require("./router/setUser");
const setPassword = require("./router/setPassword");
const setAvatar = require("./router/setAvatar");

const importUser = require("./router/importUser");
const exportUser = require("./router/exportUser");

const app = express();

// 注册功能中间件
app.use( express.static("./www") );
app.use( express.static("./upload") );
app.use( express.static("./complate") );
app.use( express.urlencoded() );
app.use( cookieParser() );
app.use( cors() );
app.use(session({
  secret: '千锋H5', // 加密信息，可以随便写
  resave: false, // 强制保存session，默认为true，建议设置为false
  saveUninitialized: true,    // 强制将未初始化的session存储，默认为true，建议为true
  cookie: { maxAge: 1000 * 20 }    // 过期时间，毫秒数
}))
// 配置可上传的文件类型
app.use( upload.any() );

// 注册路由中间件
app.get("/login", login() );
app.post("/register", register() );
app.get("/export", exportUser());

// 登录验证
app.use( verify() );

app.get("/getuser", getUser() );
// 改头像：上传文件
app.post("/setavatar", setAvatar() );
app.post("/setuser", setUser() );
app.post("/setpassword", setPassword() );

app.post("/import", importUser());
// ...

app.listen(3000);